package org.jeecg.modules.innerApi.zhiYunApi.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.common.util.MathUtil;
import org.jeecg.modules.innerApi.common.requestDto.*;
import org.jeecg.modules.innerApi.common.responseDto.*;
import org.jeecg.modules.innerApi.zhiYunApi.convert.ZyRequestParamConvert;
import org.jeecg.modules.innerApi.zhiYunApi.convert.ZyResponseRtnConvert;
import org.jeecg.modules.innerApi.zhiYunApi.requestVo.*;
import org.jeecg.modules.innerApi.zhiYunApi.responseVo.ZyFwqxxDataRtn;
import org.jeecg.modules.innerApi.zhiYunApi.responseVo.ZyFwqxxNodeRtn;
import org.jeecg.modules.innerApi.zhiYunApi.service.IZhiyunSkfpService;
import org.jeecg.modules.innerApi.zhiYunApi.util.ZhiyunApiCallUtil;
import org.jeecg.modules.innerApi.zhiYunApi.util.ZhiyunApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 新时代智云税控开票接口服务
 */
@Service
public class ZhiyunSkfpServiceImpl implements IZhiyunSkfpService {


	@Autowired
	private ZhiyunApiCallUtil zhiyunApiCallUtil;
	/**
	 * 新时代智云调用地址
	 */
	public static enum APIURL {
		znfm ("智能赋码","/intelligentCodeAssign/intelligCoding"),
		fpkj ("开具发票专票、普票、电子发票","/invoice/fillInInvoice"),
		wslgcx ("网上领购查询","/onlineLinggou/tickConllinfQuery"),
		wslglr ("网上领购录入","/onlineLinggou/onlineTicketColl"),
		hqdqwkph ("获取当前设备当票种下一张发票的号码代码","/invoice/obtainNoticNumber"),
		fpzf ("发票作废","/invoice/voidBlankInvoice"),
		hzxxbsq ("红字信息表申请","/scarletLetter/scarletLetterSq"),//向税局申请已开专票红字信息表
		hzxxbtb ("红字信息表同步","/scarletLetter/queryHzxxb"),//红字信息表查询
		hzxxbcx ("红字撤销","/scarletLetter/scarletLetterRevoke"),//撤销已经申请的红字信息表
		hzxxbsc ("红字信息表上传","/scarletLetter/hzxxb_sc"),//用于未上传的红字发票上传
		hzxxbxz ("红字信息表下载","/scarletLetter/reviewDownload"),//红字信息表下载
		fwqxx("获取服务器基本信息","/onlineClockcali/obtainServerEssInfor"),
		fpcx("发票查询","/invoiceQuery/invoiceQuerydb"),
		createPDF("生成PDF","/invoiceQuery/createPdf"),
		downloadPDF("电子普票下载PDF","/invoiceQuery/pdfDownload"),
		downloadOFD("电子普票下载OFD","/invoiceQuery/downloadOFD"),
		zpDownload("电子专票下载板式文件OFD/PDF","/invoiceQuery/dzPdfOrDown"),
		;
		private final String desc;//描述
		private final String url;//值
		APIURL (String desc, String url) {
			this.desc = desc;
			this.url = url;
		}
	}
	/***
	 * 根据税控盘机器编号
	 * 查询税控盘信息
	 * @jqbh 机器编号
	 */
	public Result<List<ZnfmRtn>> znfm(String spmc){
		ZyZnfmParam zyZnfmParam=new ZyZnfmParam();
		zyZnfmParam.setSpmc(spmc);
		ZhiyunApiResponse response=zhiyunApiCallUtil.request(APIURL.znfm.url, zyZnfmParam);
		ZyResponseRtnConvert.znfmRtnConvert(response);
		return ZyResponseRtnConvert.znfmRtnConvert(response);
	}
	/***
	 * 根据税控盘机器编号
	 * 查询税控盘信息
	 * @jqbh 机器编号
	 */
	public ZyFwqxxNodeRtn getFwqxx(GetFwqxxParam getFwqxxParam){
		ZyFwqxxDataRtn zyFwqxxDataRtn =zhiyunApiCallUtil.request(APIURL.fwqxx.url, getFwqxxParam, ZyFwqxxDataRtn.class);
		return zyFwqxxDataRtn.getJbxx().getGroup();
	}
	/***
	 * 根据税控设备、发票类型获取当前发票号、发票代码
	 *
	 * @params param
	 */
	public CurrentFphmRtn getCurrentFphm(CurrentFphmParam param){
		ZhiyunApiResponse response=zhiyunApiCallUtil.request(APIURL.hqdqwkph.url, ZyRequestParamConvert.currentFphmParamConvert(param));
		//转换返回结果
		return ZyResponseRtnConvert.currentFphmRtnConvert(response);
	}
	/***
	 * 税控发票开具
	 * 开具发票专票、普票、电子发票
	 * @params 待开票报文
	 */
	public InvoiceRtn invoice(InvoiceParam invoiceParam){
	    //转换开票参数
		ZySkfpParam param=ZyRequestParamConvert.invoiceParamConvert(invoiceParam);
		ZhiyunApiResponse response=zhiyunApiCallUtil.request(APIURL.fpkj.url, param);
		//转换开票结果
		InvoiceRtn invoiceRtn = ZyResponseRtnConvert.skfpRtnConvert(response);
		if(invoiceRtn.getSuccess()) {
			invoiceRtn.getData().setFpsqlsh(invoiceParam.getFpsqlsh());
			invoiceRtn.getData().setFplxdm(invoiceParam.getFplxdm());
			invoiceRtn.getData().setHjje(invoiceParam.getHjje());
			invoiceRtn.getData().setHjse(invoiceParam.getHjse());
			invoiceRtn.getData().setJshj(MathUtil.add(invoiceParam.getHjje(), invoiceParam.getHjse()));
		}
		return invoiceRtn;
	}
	/***
	 * 向税局申请已开专票红字信息表
	 *
	 * @params param
	 */
	public HzxxbsqRtn hzxxbsq(HzxxbsqParam param){
		ZyHzxxbsqParam zyHzxxbsqParam=ZyRequestParamConvert.hzxxbsqParamConvert(param);
		ZhiyunApiResponse response=zhiyunApiCallUtil.request(APIURL.hzxxbsq.url, zyHzxxbsqParam);
		//转换返回结果
		return ZyResponseRtnConvert.hzxxbsqRtnConvert(response);
	}
	/***
	 * 从税局同步红字信息表
	 *
	 * @params param
	 */
	public HzxxbtbRtn hzxxbtb(HzxxbtbParam param){
		ZyHzxxbtbParam zyHzxxbsqParam=ZyRequestParamConvert.hzxxbtbParamConvert(param);
		String response=zhiyunApiCallUtil.requestStr(APIURL.hzxxbtb.url, zyHzxxbsqParam);
		//转换返回结果
		return ZyResponseRtnConvert.hzxxbtbRtnConvert(response);
	}
	/***
	 * 红字信息表撤销
	 *
	 * @params param
	 */
	public CommonRtn hzxxbcx(HzxxbcxParam param){
		ZyHzxxbcxParam zyHzxxbcxParam=ZyRequestParamConvert.hzxxbcxParamConvert(param);
		ZhiyunApiResponse response=zhiyunApiCallUtil.request(APIURL.hzxxbcx.url, zyHzxxbcxParam);
		//转换返回结果
		return ZyResponseRtnConvert.commonRtnConvert(response);
	}
	/***
	 * 红字信息表上传
	 *
	 * @params param
	 */
	public CommonRtn hzxxbsc(HzxxbcxParam param){
		ZyHzxxbcxParam zyHzxxbcxParam=ZyRequestParamConvert.hzxxbcxParamConvert(param);
		ZhiyunApiResponse response=zhiyunApiCallUtil.request(APIURL.hzxxbsc.url, zyHzxxbcxParam);
		//转换返回结果
		return ZyResponseRtnConvert.commonRtnConvert(response);
	}
	/***
	 * 红字信息表下载
	 *
	 * @params param
	 */
	public FileStreamRtn hzxxbxz(HzxxbxzParam param){
		ZyHzxxbxzParam zyHzxxbcxParam=ZyRequestParamConvert.hzxxbxzParamConvert(param);
		//转换返回结果
		return zhiyunApiCallUtil.requestDownFlie(APIURL.hzxxbxz.url, zyHzxxbcxParam);
	}

	@Override
	public CommonRtn createPdf(CreatePdfParam param) {
		ZyCreatePdfParam zyCreatePdfParam=ZyRequestParamConvert.createPdfParamConvert(param);
		//转换返回结果
		ZhiyunApiResponse response = zhiyunApiCallUtil.request(APIURL.createPDF.url, zyCreatePdfParam);
		return ZyResponseRtnConvert.commonRtnConvert(response);
	}

	/**
	 * 下载电子普票PDF
	 * @param fplxdm
	 * @param ids
	 * @return
	 */
	@Override
	public FileStreamRtn downloadPdf(String fplxdm, List<String> ids) {
		ZyDownloadPdfParam zyDownloadPdfParam = new ZyDownloadPdfParam();
		zyDownloadPdfParam.setFplxdm(fplxdm);
		zyDownloadPdfParam.setIds(StringUtils.join(ids,","));

		return zhiyunApiCallUtil.requestDownFlie(APIURL.downloadPDF.url, zyDownloadPdfParam);
	}

	/**
	 * 下载电子普票OFD
	 * @param fpdm
	 * @param fphm
	 * @param jqbh
	 * @return
	 */
	@Override
	public FileStreamRtn downloadOfd(String fpdm, String fphm, String jqbh) {
		ZyDownloadOfdParam zyDownloadOfdParam = new ZyDownloadOfdParam();
		zyDownloadOfdParam.setFpdm(fpdm);
		zyDownloadOfdParam.setFphm(fphm);
		zyDownloadOfdParam.setJqbh(jqbh);
		return zhiyunApiCallUtil.requestDownFlie(APIURL.downloadOFD.url, zyDownloadOfdParam);
	}

	/**
	 * 电子专票版式文件下载OFD/PDF
	 * @param fpdm
	 * @param fphm
	 * @param downflag
	 * @return
	 */
	@Override
	public FileStreamRtn zpDownload(String fpdm, String fphm, String downflag) {
		ZyZpDownloadParam zyZpDownloadParam = new ZyZpDownloadParam();
		zyZpDownloadParam.setFpdm(fpdm);
		zyZpDownloadParam.setFphm(fphm);
		zyZpDownloadParam.setDownflag(downflag);
		return zhiyunApiCallUtil.requestDownFlie(APIURL.zpDownload.url, zyZpDownloadParam);
	}

	/**
	 * 发票查询
	 * @param param
	 * @return
	 */
	@Override
	public InvoiceQueryRtn invoiceQuery(InvoiceQueryParam param) {
		ZyInvoiceQueryParam zyInvoiceQueryParam=ZyRequestParamConvert.invoiceQueryParamConvert(param);
		//转换返回结果
		String result = zhiyunApiCallUtil.requestStr(APIURL.fpcx.url, zyInvoiceQueryParam);
		return ZyResponseRtnConvert.invoiceQueryRtnConvert(result);
	}

	/***
	 * 发票作废
	 * @params 作废发票信息
	 */
	public FpzfRtn fpzf(FpzfParam param){
		ZyFpzfParam zyFpzfParam = ZyRequestParamConvert.fpzfParamConvert(param);
		ZhiyunApiResponse response=zhiyunApiCallUtil.request(APIURL.fpzf.url, zyFpzfParam);
		//转换开票结果
		return ZyResponseRtnConvert.fpzfRtnConvert(response);
	}


}
